﻿using EasyCore.UnifyResult.Results;
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging.Abstractions;
using SharpGrip.FluentValidation.AutoValidation.Mvc.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.DataValidation
{
    public class ParameterErrorResultFactory : IFluentValidationAutoValidationResultFactory
    {
        public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
        {
            Dictionary<string, object> errorData = new Dictionary<string, object>();
            if (validationProblemDetails != null)
            {
                foreach (var error in validationProblemDetails.Errors)
                {
                    errorData.Add(error.Key, error.Value[0]);
                }
            }
            var result = new EasyApiResult((int)EasyStatusCodes.ParametersError, "参数验证失败", errorData);
            return new JsonResult(result);
        }
    }
}
